home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00082_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  9.4 KB  |  340 lines

  1. /*
  2.  * @(#)ImageTest.java    1.5 95/11/30
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31. import java.awt.*;
  32. import java.awt.image.*;
  33. import sun.awt.image.URLImageSource;
  34. import java.applet.Applet;
  35.  
  36. public class ImageTest extends Applet {
  37.     public void init() {
  38.     setLayout(new BorderLayout());
  39.     add("Center", new ImagePanel(this));
  40.     add("North", new ImageHelp());
  41.     reshape(0, 0, 800, 600);
  42.     show();
  43.     }
  44. }
  45.  
  46. class ImageHelp extends Panel {
  47.     public ImageHelp() {
  48.     setLayout(new GridLayout(0, 2));
  49.     add(new Label("Move the images using the arrow keys",
  50.               Label.CENTER));
  51.     add(new Label("Resize the images using the PgUp/PgDn keys",
  52.               Label.CENTER));
  53.     add(new Label("Toggle a red/blue color filter using the Home key",
  54.               Label.CENTER));
  55.     add(new Label("Change the alpha using the shifted PgUp/PgDn keys",
  56.               Label.CENTER));
  57.     }
  58. }
  59.  
  60. class ImagePanel extends Panel {
  61.     Applet applet;
  62.  
  63.     public ImagePanel(Applet app) {
  64.     applet = app;
  65.     setLayout(new BorderLayout());
  66.     Panel grid = new Panel();
  67.     grid.setLayout(new GridLayout(0, 2));
  68.     add("Center", grid);
  69.     grid.add(new ImageCanvas(applet, makeDitherImage(), 0.5));
  70.     Image joe = applet.getImage(applet.getDocumentBase(),
  71.                     "graphics/joe.surf.yellow.small.gif");
  72.     grid.add(new ImageCanvas(applet, joe, 1.0));
  73.     reshape(0, 0, 20, 20);
  74.     }
  75.  
  76.     Image makeDitherImage() {
  77.     int w = 100;
  78.     int h = 100;
  79.     int pix[] = new int[w * h];
  80.     int index = 0;
  81.     for (int y = 0; y < h; y++) {
  82.         int red = (y * 255) / (h - 1);
  83.         for (int x = 0; x < w; x++) {
  84.         int blue = (x * 255) / (w - 1);
  85.         pix[index++] = (255 << 24) | (red << 16) | blue;
  86.         }
  87.     }
  88.     return applet.createImage(new MemoryImageSource(w, h, pix, 0, w));
  89.     }
  90. }
  91.  
  92. class ImageCanvas extends Canvas implements ImageObserver {
  93.     double     hmult = 0;
  94.     int        xadd = 0;
  95.     int        yadd = 0;
  96.     int        imgw = -1;
  97.     int        imgh = -1;
  98.     int        scalew = -1;
  99.     int        scaleh = -1;
  100.     boolean    focus = false;
  101.     boolean    usefilter = false;
  102.     static final int numalphas = 8;
  103.     int        alpha = numalphas - 1;
  104.     Image    imagevariants[] = new Image[numalphas * 2];
  105.     ImageFilter colorfilter;
  106.     Image    origimage;
  107.     Image    curimage;
  108.     Applet    applet;
  109.  
  110.     public ImageCanvas(Applet app, Image img, double mult) {
  111.     applet = app;
  112.     origimage = img;
  113.     imagevariants[numalphas - 1] = origimage;
  114.     hmult = mult;
  115.     pickImage();
  116.     reshape(0, 0, 100, 100);
  117.     }
  118.  
  119.     public boolean gotFocus(Event e, Object arg) {
  120.     focus = true;
  121.     repaint();
  122.     return true;
  123.     }
  124.  
  125.     public boolean lostFocus(Event e, Object arg) {
  126.     focus = false;
  127.     repaint();
  128.     return true;
  129.     }
  130.  
  131.     public void paint(Graphics g) {
  132.     Rectangle r = bounds();
  133.     int hlines = r.height / 10;
  134.     int vlines = r.width / 10;
  135.  
  136.     if (focus) {
  137.         g.setColor(Color.red);
  138.     } else {
  139.         g.setColor(Color.darkGray);
  140.     }
  141.     g.drawRect(0, 0, r.width-1, r.height-1);
  142.     g.drawLine(0, 0, r.width, r.height);
  143.     g.drawLine(r.width, 0, 0, r.height);
  144.     g.drawLine(0, r.height / 2, r.width, r.height / 2);
  145.     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  146.     if (imgw < 0) {
  147.         imgw = curimage.getWidth(this);
  148.         imgh = curimage.getHeight(this);
  149.         if (imgw < 0 || imgh < 0) {
  150.         return;
  151.         }
  152.     }
  153.     if (scalew < 0) {
  154.         scalew = (int) (imgw * hmult);
  155.         scaleh = (int) (imgh * hmult);
  156.     }
  157.     if (imgw != scalew || imgh != scaleh) {
  158.         g.drawImage(curimage, xadd, yadd, scalew, scaleh, this);
  159.     } else {
  160.         g.drawImage(curimage, xadd, yadd, this);
  161.     }
  162.  
  163.     }
  164.  
  165.     static final long updateRate = 100;
  166.  
  167.     public synchronized boolean imageUpdate(Image img, int infoflags,
  168.                         int x, int y, int w, int h) {
  169.     if (img != curimage) {
  170.         return false;
  171.     }
  172.     boolean ret = true;
  173.     boolean dopaint = false;
  174.     long updatetime = 0;
  175.     if ((infoflags & WIDTH) != 0) {
  176.         imgw = w;
  177.         dopaint = true;
  178.     }
  179.     if ((infoflags & HEIGHT) != 0) {
  180.         imgh = h;
  181.         dopaint = true;
  182.     }
  183.     if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
  184.         dopaint = true;
  185.         ret = false;
  186.     } else if ((infoflags & SOMEBITS) != 0) {
  187.         dopaint = true;
  188.         updatetime = updateRate;
  189.     }
  190.     if ((infoflags & ERROR) != 0) {
  191.         ret = false;
  192.     }
  193.     if (dopaint) {
  194.         repaint(updatetime);
  195.     }
  196.     return ret;
  197.     }
  198.  
  199.     public synchronized Image pickImage() {
  200.     int index = alpha;
  201.     if (usefilter) {
  202.         index += numalphas;
  203.     }
  204.     Image choice = imagevariants[index];
  205.     if (choice == null) {
  206.         choice = imagevariants[alpha];
  207.         if (choice == null) {
  208.         int alphaval = (alpha * 255) / (numalphas - 1);
  209.         ImageFilter imgf = new AlphaFilter(alphaval);
  210.         ImageProducer src = origimage.getSource();
  211.         src = new FilteredImageSource(src, imgf);
  212.         choice = applet.createImage(src);
  213.         imagevariants[alpha] = choice;
  214.         }
  215.         if (usefilter) {
  216.         if (colorfilter == null) {
  217.             colorfilter = new RedBlueSwapFilter();
  218.         }
  219.         ImageProducer src = choice.getSource();
  220.         src = new FilteredImageSource(src, colorfilter);
  221.         choice = applet.createImage(src);
  222.         }
  223.         imagevariants[index] = choice;
  224.     }
  225.     curimage = choice;
  226.     return choice;
  227.     }
  228.  
  229.     public synchronized boolean handleEvent(Event e) {
  230.     switch (e.id) {
  231.       case Event.MOUSE_ENTER:
  232.         requestFocus();
  233.         return true;
  234.  
  235.       case Event.KEY_ACTION:
  236.       case Event.KEY_PRESS:
  237.         switch (e.key) {
  238.           case Event.HOME:
  239.         usefilter = !usefilter;
  240.         pickImage();
  241.         repaint();
  242.         return true;
  243.           case Event.UP:
  244.         yadd -= 5;
  245.         repaint();
  246.         return true;
  247.           case Event.DOWN:
  248.         yadd += 5;
  249.         repaint();
  250.         return true;
  251.           case Event.RIGHT:
  252.           case 'r':
  253.         xadd += 5;
  254.         repaint();
  255.         return true;
  256.           case Event.LEFT:
  257.         xadd -= 5;
  258.         repaint();
  259.         return true;
  260.           case Event.PGUP:
  261.         if ((e.modifiers & Event.SHIFT_MASK) != 0) {
  262.             if (++alpha > numalphas - 1) {
  263.             alpha = numalphas - 1;
  264.             }
  265.             pickImage();
  266.         } else {
  267.             hmult *= 1.2;
  268.         }
  269.         scalew = scaleh = -1;
  270.         repaint();
  271.         return true;
  272.           case Event.PGDN:
  273.         if ((e.modifiers & Event.SHIFT_MASK) != 0) {
  274.             if (--alpha < 0) {
  275.             alpha = 0;
  276.             }
  277.             pickImage();
  278.         } else {
  279.             hmult /= 1.2;
  280.         }
  281.         scalew = scaleh = -1;
  282.         repaint();
  283.         return true;
  284.           default:
  285.         return super.handleEvent(e);
  286.         }
  287.  
  288.       default:
  289.         return super.handleEvent(e);
  290.     }
  291.     }
  292.  
  293. }
  294.  
  295. class RedBlueSwapFilter extends RGBImageFilter {
  296.     public RedBlueSwapFilter() {
  297.     canFilterIndexColorModel = true;
  298.     }
  299.  
  300.     public void setColorModel(ColorModel model) {
  301.     if (model instanceof DirectColorModel) {
  302.         DirectColorModel dcm = (DirectColorModel) model;
  303.         int rm = dcm.getRedMask();
  304.         int gm = dcm.getGreenMask();
  305.         int bm = dcm.getBlueMask();
  306.         int am = dcm.getAlphaMask();
  307.         int bits = dcm.getPixelSize();
  308.         dcm = new DirectColorModel(bits, bm, gm, rm, am);
  309.         substituteColorModel(model, dcm);
  310.         consumer.setColorModel(dcm);
  311.     } else {
  312.         super.setColorModel(model);
  313.     }
  314.     }
  315.  
  316.     public int filterRGB(int x, int y, int rgb) {
  317.     return ((rgb & 0xff00ff00)
  318.         | ((rgb & 0xff0000) >> 16)
  319.         | ((rgb & 0xff) << 16));
  320.     }
  321. }
  322.  
  323. class AlphaFilter extends RGBImageFilter {
  324.     ColorModel origmodel;
  325.     ColorModel newmodel;
  326.  
  327.     int alphaval;
  328.  
  329.     public AlphaFilter(int alpha) {
  330.     alphaval = alpha;
  331.     canFilterIndexColorModel = true;
  332.     }
  333.  
  334.     public int filterRGB(int x, int y, int rgb) {
  335.     int alpha = (rgb >> 24) & 0xff;
  336.     alpha = alpha * alphaval / 255;
  337.     return ((rgb & 0x00ffffff) | (alpha << 24));
  338.     }
  339. }
  340.